//* Copyright(C) 2005-2014, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
 **************************************************************************
 */

/**
 * 文档信息插件类；
 */
define(function (require, exports, module) {
    /**
     * Plugin of document properties.
     * @param {WebPDF.ReaderApp} readerApp app instance
     * @extends {WebPDF.IReaderPlugin}
     * @constructor
     */
    WebPDF.PDFDocPropertiesPlugin = function (readerApp) {
        var _readerApp = readerApp,
            _appId = _readerApp.getAppId(),
            _jqPanel = null,
            _self = this;

        var _fieldIDs = {
            author: _appId + "_docPropertiesAuthor",
            creator: _appId + "_docPropertiesCreator",
            producer: _appId + "_docPropertiesProducer",
            title: _appId + "_docPropertiesTitle",
            subject: _appId + "_docPropertiesSubject",
            keywords: _appId + "_docPropertiesKeywords",
            creationDate: _appId + "_docPropertiesCreationDate",
            modifyDate: _appId + "_docPropertiesModifyDate",
            version: _appId + "_docPropertiesVersion",
            pageCount: _appId + "_docPropertiesPageCount",
            pageSize: _appId + "_docPropertiesPageSize",
            unit: _appId + "_docPropertiesUnit"
        };

        /**
         * Convert value from point to target unit.
         * @param targetUnit target unit
         * @param value value to convert
         * @returns {Number}
         * @private
         */
        function _convertValueFromPointUnit(targetUnit, value) {
            var ptValue = WebPDF.Common.changeUnit(value, "point", targetUnit);
            return WebPDF.Common.parseValueToDecimal(ptValue, 2);
        }

        //获取Plugin名称;
        this.getPluginName = function () {
            return "DocProperties Plugin";
        };
        //插件被注册的时候调用;
        this.onRegister = function () {
        };
        //模块初始化;
        this.init = function () {
            _jqPanel = $('#' + _self.getPanelID());
            _createHtml();
        };
        //模块卸载;
        this.unload = function () {
        };

        //下面实现 INavigationChildPanel接口;
        this.getPanelID = function () {
            return _appId + "_fxDocPropertiesPanel";
        };
        this.getTabBtnID = function () {
            return _appId + "_dptab";
        };
        this.getTabNorImgUrl = function () {
            return "";
        };
        this.getTabHotImgUrl = function () {
            return "";
        };
        this.getTitle = function () {
            return "DocProperties.TitleMobile";
        };
        this.onSize = function (panelWidth, panelHeight) {
            if(!_jqPanel) return;
            _jqPanel.css({ width: panelWidth, height: panelHeight });
        };
        //end INavigationChildPanel接口;

        function _createHtml() {
            var docProperties = _readerApp.getPDFDoc().getDocProperties();
            if (!docProperties) {
                return;
            }

            var fileName=_readerApp.getFileName();
            var propertiesId = _appId + "_fwr_doc_properties";
            var docInfoHtml = '<div id="' + propertiesId + '" style="display: block;width: auto;padding:30px 15px;color:#020202;font-size:20px;line-height:28px;">' +
                    '<div><span style="font-weight: bold;" data-i18n="DocProperties.FilenName"></span><span style="word-break: break-word;" title="' + fileName  +  '">' + fileName + '</span></div>' +
                    '<div><span style="font-weight: bold;" data-i18n="DocProperties.PDFTitle"></span><span style="word-break: break-word;" id="' + _fieldIDs.title  +  '"></span></div>' +
                    '<div><span style="font-weight: bold;" data-i18n="DocProperties.Subject"></span><span style="word-break: break-word;" id="' + _fieldIDs.subject  +  '"></span></div>' +
                    '<div><span style="font-weight: bold;" data-i18n="DocProperties.Author"></span><span style="word-break: break-word;" id="' + _fieldIDs.author  +  '"></span></div>' +
                    '<div><span style="font-weight: bold;" data-i18n="DocProperties.Creator"></span><span style="word-break: break-word;" id="' + _fieldIDs.creator  +  '"></span></div>' +
                    '<div><span style="font-weight: bold;" data-i18n="DocProperties.Producer"></span><span style="word-break: break-word;" id="' + _fieldIDs.producer  +  '"></span></div>' +
                    '<div><span style="font-weight: bold;" data-i18n="DocProperties.Keywords"></span><span style="word-break: break-word;" id="' + _fieldIDs.keywords  +  '"></span></div>' +
                    '<div><span style="font-weight: bold;" data-i18n="DocProperties.CreateDate"></span><span style="word-break: break-word;" id="' + _fieldIDs.creationDate  +  '"></span></div>' +
                    '<div><span style="font-weight: bold;" data-i18n="DocProperties.ModifyDate"></span><span style="word-break: break-word;" id="' + _fieldIDs.modifyDate  +  '"></span></div>' +
                    '<div style="margin-top:30px;"><span style="font-weight: bold;" data-i18n="DocProperties.PDFVersion"></span><span style="word-break: break-word;" id="' + _fieldIDs.version  +  '"></span></div>' +
                    '<div><span style="font-weight: bold;" data-i18n="DocProperties.PageSize"></span><span style="word-break: break-word;" id="' + _fieldIDs.pageSize  +  '"></span>' +
                           '<span id="' + _fieldIDs.unit + '" style="margin-left:4px;word-break: break-word;" data-i18n="DocProperties.Inch"></span>' +
/*                        '<select id="' + _fieldIDs.unit + '" style="width:53px;margin-left:4px">' +
                            '<option value="inch" data-i18n="DocProperties.Inch"></option>' +
                            '<option value="cm" data-i18n="DocProperties.Centimeter"></option>' +
                            '<option value="point" data-i18n="DocProperties.Point"></option>' +
                            '<option value="pica" data-i18n="DocProperties.Pica"></option>' +
                        '</select>' +*/
                    '</div>' +
                    '<div><span style="font-weight: bold;" data-i18n="DocProperties.NumOfPages"></span><span style="word-break: break-word;" id="' + _fieldIDs.pageCount + '"></span></div>' +
                '</div>';

            _jqPanel.append(docInfoHtml);
  /*          $("#" + _fieldIDs.unit).on("change", function () {
                _onChangeUnit();
            });*/

            $("#" + propertiesId).i18n();
            var authorTitle = docProperties.getAuthor();
            if (authorTitle) {
                $("#" + _fieldIDs.author).text(authorTitle).attr("title", authorTitle);
            }
            var creatorTitle = docProperties.getCreator();
            if (creatorTitle) {
                $("#" + _fieldIDs.creator).text(creatorTitle).attr("title", creatorTitle);
            }
            var producerTitle = docProperties.getProducer();
            if (producerTitle) {
                $("#" + _fieldIDs.producer).text(producerTitle).attr("title", producerTitle);
            }
            var titleTitle = docProperties.getTitle();
            if (titleTitle) {
                $("#" + _fieldIDs.title).text(titleTitle).attr("title", titleTitle);
            }
            var subjectTitle = docProperties.getSubject();
            if (subjectTitle) {
                $("#" + _fieldIDs.subject).text(subjectTitle).attr("title", subjectTitle);
            }
            var keywordsTitle = docProperties.getKeywords();
            if (keywordsTitle) {
                $("#" + _fieldIDs.keywords).val(keywordsTitle);
            }
            var creationDate = docProperties.getCreationDate();
            if (creationDate) {
                $("#" + _fieldIDs.creationData).text(creationDate).attr("title", creationDate);
            }
            var modifyDate = docProperties.getModifyDate();
            if (modifyDate) {
                $("#" + _fieldIDs.modifyDate).text(modifyDate).attr("title", modifyDate);
            }
            var version = docProperties.getVersion();
            if (version) {
                $("#" + _fieldIDs.version).text(version).attr("title", version);
            }
            var pageCount = _readerApp.getMainView().getDocView().getPageCount();
            $("#" + _fieldIDs.pageCount).text(pageCount).attr("title", pageCount);
            _onChangeUnit();
        }

        function _onChangeUnit() {
            var unit = $('#' + _fieldIDs.unit).val();
            if (!unit) {
                unit = $('#' + _fieldIDs.unit).html();
            }
            var curPageIndex = _readerApp.getMainView().getDocView().getCurPageIndex();
            var pdfPage = _readerApp.getPDFDoc().getPage(curPageIndex);
            var w = _convertValueFromPointUnit(unit, pdfPage.getPageWidth());
            var h = _convertValueFromPointUnit(unit, pdfPage.getPageHeight());
            var pageSize = w + " * " + h;
            $("#" + _fieldIDs.pageSize).text(pageSize).attr("title", pageSize);
        }
    };

    exports.createPlugin = function (readerApp) {
        return new WebPDF.PDFDocPropertiesPlugin(readerApp);
    };
});